<?php
namespace App\Http\Controllers\Admin\Qudao;

use App\Service\Admin\Qudao\CardService;
use Illuminate\Http\Request;
use App\Utilitys\Upload;

class CardController extends BaseController
{
    //渠道商

    public function __construct(CardService $service)
    {
        $this->db = $service;
    }

    //选项
    public function getOption(Request $request)
    {
        $res = $this->db->getOption();
        return $this->toJson(0, '', $res);
    }

    //列表
    public function getIndex(Request $request)
    {
        $res = $this->db->getList();
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //详情
    public function getOne(Request $request)
    {
        $id = $request->post('id', 0);
        $res = $this->db->getOne($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //修改
    public function modify(Request $request)
    {
        $id = $request->post('id', 0);
        $pas['cate_id'] = $request->post('cateid', 0);
        $pas['title'] = $request->post('title', '');
        $pas['img_url'] = $request->post('img', '');
        if ($id) {
            $res = $this->db->modify($id, $pas);
        } else {
            $res = $this->db->add($pas);
        }
        if ($res['code'] < 0) {
            return $this->toJson($res['code'], $res['msg']);
        }
        return $this->toJson();
    }

    //删除
    public function setDel(Request $request)
    {
        $id = $request->post('id', 0);
        $res = $this->db->setDel($id);
        if ($res['code'] < 0) {
            return $this->toJson($res['code'], $res['msg']);
        }
        return $this->toJson();
    }

    //商品列表
    public function goodsIndex(Request $request)
    {
        $res = $this->db->goodsList();
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //转赠列表
    public function userIndex(Request $request)
    {
        $pages['start'] = $request->post('start', 1);
        $pages['limit'] = $request->post('limit', 10);
        $res = $this->db->userList($pages);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //订单列表
    public function orderList(Request $request)
    {
        $pages['start'] = $request->post('start', 1);
        $pages['limit'] = $request->post('limit', 10);
        $res = $this->db->orderList($pages);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //分类列表
    public function cateList(Request $request)
    {
        $res = $this->db->cateList();
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //分类修改
    public function cateModify(Request $request)
    {
        $id = $request->post('id', 0);
        $cate_name = $request->post('cname', '');
        $res = $this->db->cateModify($id, $cate_name);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //分类删除
    public function cateDel(Request $request)
    {
        $id = $request->post('id', 0);
        $res = $this->db->cateDel($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //商品选项
    public function goodsOption(Request $request)
    {
        return $this->toJson(0, '', $this->db->goodsOption());
    }

    //商品修改
    public function goodsModify(Request $request)
    {
        $id = $request->post('id', 0);
        $pas['card_id'] = $request->post('cardid', 0);
        $pas['title'] = $request->post('title', '');
        $pas['price'] = $request->post('price', 0);
        $pas['num'] = $request->post('num', 0);
        $res = $this->db->goodsModify($id, $pas);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //商品删除
    public function goodsDel(Request $request)
    {
        $id = $request->post('id', 0);
        $res = $this->db->goodsDel($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //上传
    public function upload(Request $request)
    {
        $id = $request->post('id', 0);
        $img = $request->post('img', '');
//        if (!$id) {
//            return $this->toJson(-2, 'ID必须的');
//        }
        if (!$img) {
            return $this->toJson(-2, '图片必须的');
        }
        $fpath = Upload::getPathByBase64($img);
        $sizes = getimagesize($img);
        $res = $this->db->addImg($id, $fpath, $sizes[0], $sizes[1]);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        $res = $this->db->imgList($id);
        return $this->toJson(0, '', $res);
    }
}